<style lang="scss" scoped>
.btn {
  position: absolute;
  bottom: 50px;
  width: 330px;
  left: 50%;
  transform: translateX(-50%);
}
</style>
<template>
  <div>
    <van-nav-bar left-arrow title="修改密码" @click-left="router.push('/manage')"></van-nav-bar>
    <el-form label-width="90px" style="padding: 10px" :model="form">
      <el-form-item prop="oldPwd" label="我的密码">
        <el-input v-model="form.oldPwd" placeholder="请输入原密码"></el-input>
      </el-form-item>
      <el-form-item prop="newPwd" label="设置新密码">
        <el-input v-model="form.newPwd" placeholder="请输入新密码"></el-input>
      </el-form-item>
      <el-form-item prop="newPwd1" label="确认新密码">
        <el-input v-model="form.newPwd1" placeholder="请再次输入密码"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="large" class="btn" @click="submit">确认</el-button>
  </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { JSEncrypt } from 'jsencrypt'
import { ElMessage } from 'element-plus'
import { request } from '../../axios/request'
// 路由
const router = useRouter()

// 数据绑定
const form = ref({
  oldPwd: '',
  newPwd: '',
  newPwd1: ''
})

// 拿取userId, sessionId
const userId = JSON.parse(localStorage.getItem('userId')|| '') 
const sessionId = JSON.parse(localStorage.getItem('sessionId')|| '') 

// 提交
const submit = async () => {
  console.log(form.value)
  const crypt = new JSEncrypt()
  const publicKey =
    'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuQd3gESx7VdIyRYUWjmjg61VIgUK6F45hClmqUMZ7xNiT5rlLM6e78osMvBF/yP7cVm7pK+NMCDWoVS1/AETpxJYqUlIC77ZAU8/MnP96IupnJL87vqhPcpdv7+VqLM38ls++yuD/F/HSoOQTv/leJh+dgE/4EYAJjOWFAbYfXwIDAQAB'
  // let publicKey = 'KoZIhvcNAQwxOggaWPsYQJT+kpWZ/SpshZzmB=='
  crypt.setPublicKey(publicKey)
  const Pwd1 = ref(crypt.encrypt(form.value.oldPwd))
  const Pwd2 = ref(crypt.encrypt(form.value.newPwd))
  console.log(Pwd2.value)
  console.log(Pwd1.value)
  if (form.value.newPwd == form.value.newPwd1) {
    await request.put(
      '/api/health/user/verify/v1/updateUserPwd',
      {
        oldPwd: Pwd1.value,
        newPwd: Pwd2.value
      },
      {
        headers: {
          userId: userId.value as number,
          sessionId: sessionId.value as string,
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }
    ).then(res => {
        console.log(res);
        let { message, status } = res.data
        if (status == '0000') {
            ElMessage.success(message)
            router.back()
        }else {
          ElMessage.error(message)
        }
    })
  } else {
    ElMessage.error('两次密码不一致')
  }
}
</script>